/**
 * 主ui
 */
class HomeView extends BaseEuiView {
	private vj: VirtualJoystick = new VirtualJoystick();  //虚拟摇杆
	private player: eui.Rect;    //人物
	private speedX = 0;         //人物移动速度
	private speedY = 0;
	private speed = 10;
	public constructor($controller: BaseController, $parent: eui.Group) {
		super($controller, $parent);
		this.skinName = "resource/skins/MapSkin.exml";
	}
	public initUI(): void {
		super.initUI();
		this.addChild(this.vj);
		//开启虚拟摇杆
		this.vj.start();
		this.vj.addEventListener("vj_start", this.onStart, this);
		this.vj.addEventListener("vj_move", this.onChange, this);
		this.vj.addEventListener("vj_end", this.onEnd, this);
		this.player = new eui.Rect();
		this.player.x = 789.33;
		this.player.y = 136
		this.player.width = 100;
		this.player.height = 100;
		this.player.fillColor = 0x30f221;
		this.addChild(this.player);
	}

	//摇杆启动，人物开始根据摇杆移动
	private onStart() {
	   this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	//触摸摇杆的角度改变，人物的移动速度方向也随之改变
	private onChange(e: egret.Event) {
		var angle = e.data;
		this.speedX = Math.cos(angle) * this.speed;
		this.speedY = Math.sin(angle) * this.speed;
	}

	//停止摇杆，人物停止移动
	private onEnd() {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	//每帧更新，人物移动
	private onEnterFrame() {
		this.player.x += this.speedX;
		this.player.y += this.speedY;
	}
}